package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.adapter.MyFollowAdapter;
import com.hxkcloud.patient.beans.MyFollowBean;
import com.hxkcloud.patient.beans.MyFollowListBean;
import com.hxkcloud.patient.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/14.
 */
public class MyFollowActivity extends BaseActivity implements View.OnClickListener,PullToRefreshLayout.OnRefreshListener,MyFollowAdapter.ResultSelectedListener {


    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recycleView;

    private List<MyFollowBean> followList;
    private MyFollowAdapter adapter;

    private ObjectResultBean<MyFollowListBean> resultBean;

    private LinearLayout emptyLayout;
    private TextView emptyTV;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_MYFOLLOWUPLISTINFO_SUCCESS:
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<MyFollowListBean>) msg.obj;
                    if(resultBean.getData().getFollowUpList()!=null){
                        followList = resultBean.getData().getFollowUpList();
                        if(followList!=null && followList.size()>0){
                            adapter.setData(followList);
                            emptyLayout.setVisibility(View.GONE);
                        }else{
                            refreshLayout.setVisibility(View.GONE);
                            emptyLayout.setVisibility(View.VISIBLE);
                        }
                    }

                }

                break;
            case MessageCode.NETWORKCODE_MYFOLLOWUPLISTINFO_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_myfollow);
        followList = new ArrayList<MyFollowBean>();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.myfollow_text_title));
        iv_back.setOnClickListener(this);


        refreshLayout = (PullToRefreshLayout) findViewById(R.id.myfollow_refreshlayout);
        recycleView = (PullableRecyclerView) findViewById(R.id.myfollow_recyclerview);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);

        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(mLayoutManager);

        adapter = new MyFollowAdapter(this, followList, recycleView, this);
        recycleView.setAdapter(adapter);

        emptyLayout = (LinearLayout) findViewById(R.id.ll_department_empty);
        emptyTV = (TextView) findViewById(R.id.empty_text);
        emptyTV.setText(getResources().getString(R.string.empty_text_follow));

    }


    @Override
    protected void initListener() {

        iv_back.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);

    }

    @Override
    protected void initData() {
        YiJiaHuNetWorkUtils.myFollowUp(handler);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;

        }

    }


    @Override
    public void onSelected(String type, MyFollowBean info) {
        if("click".equals(type)){
            Intent intent = new Intent(this,
                    MyFollowDetailActivity.class);
            intent.putExtra("questionUserId", info.getAnswerId());
            startActivity(intent);

        }

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .refreshFinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout
                .loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }
}
